var QGSDK = (function () {
    function QGSDK(param) {
        this.openid = this._getQueryByName("openid");
        this.openkey = this._getQueryByName("openkey");
        this.token = this._getQueryByName("access_token");
        this.pf = this._getQueryByName("pf");
        this.pfkey = this._getQueryByName("pfkey");
        this.param = this._getQueryByName("param");
        this.appid = param.appId;
        this.appKey = param.appKey;
        this.kpappid = param.other;
        this.spId = param.sp;
        this.gameId = param.gameId;
        this.myOpenKey = param.openKey;
    }
    QGSDK.prototype._getQueryByName = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null)
            return decodeURIComponent(r[2]);
        return "";
    };
    QGSDK.prototype.login = function (data, callback) {
        var result = {
            result: 0,
            desc: "login sussecc",
            sessionKey: this.openkey,
            unionUserId: this.openid,
            spuid: this.openid,
            avatarUrl: "",
            nickName: "",
            other: window.location.search.substring(1)
        };
        callback && callback(result);
    };
    QGSDK.prototype.invite = function (data, callback) {
        var inviteParm = {
            title: data.title,
            tabid: 0,
            gamepara: data.customMeta,
            tips_id: data.tipsId
        };
        this.gApi.InviteBox.show(this.appid, inviteParm, function (data) {
            if (callback)
                callback({ result: -1, desc: data.resultstr });
        }, function (data) {
            if (callback)
                callback({ result: data.result, desc: data.resultstr });
        });
    };
    QGSDK.prototype.share = function (data, callback) {
    };
    QGSDK.prototype.pay = function (data, callback) {
        console.log(">>>" + JSON.stringify(data));
        console.log(">>>>data.data.url" + data.data.url);
        this.gApi.BuyBox.show(this.appid, data.data.url, 0, "支付", function () {
            if (callback)
                callback({ result: -1, desc: "pay cancle" });
        }, function () {
            if (callback)
                callback({ result: 0, desc: "pay sussecc" });
        });
    };
    QGSDK.prototype.openVIPService = function (data, callback) {
        this.gApi.NewOpenGameVIPService.show(this.appid, function () {
            callback({ result: -1, desc: "close VIPService" });
        }, "VIP.APP" + this.kpappid + ".PLATqqgamemini");
    };
    var OPENVIP_URL = "http://paycenter.layabox.com/pay/88/gettoken";
    QGSDK.prototype.openVipAction = function (data, callback) {
        var m_this = this;
        var newdata = "layaappkey="+this.myOpenKey+"&suserid="+this.openid+"&iteminfo="+data.itemInfo+"&sp="+this.spId+"&gameid="+this.gameId+"&stoken="+this.token+"&ext="+JSON.stringify(data.ext)+"&pf="+this.pf+"&zoneid="+data.zoneid+"&pfkey="+this.pfkey+"&tokentype="+data.tokentype;
        LayaCommon.getPostJson(encodeURIComponent(OPENVIP_URL),encodeURIComponent(newdata),"GET",this.token,function(res){
            var myData = JSON.parse(res.data[0]);
            if(myData && myData.rid == 0)
            {
                callback&&callback&&callback({result: 0, token:myData.data.token,desc: "OK"});
                m_this.gApi.GameVIPAction.show
                (
                    m_this.kpappid,
                    data.mp_id,
                    myData.data.token,
                    data.zoneid,
                    m_this.openid,
                    'v3',
                    // 回调函数
                    function() {callback&&callback({result: -1, desc: "close VipAction"})}
                );
            }else {
                callback&&callback({result: -1, desc: "code"+myData.rid+",msg"+myData.msg});
            }
        })
    };
    return QGSDK;
}());
var SPSdk = (function () {
    function SPSdk() {
        this.urls = ["http://qqgameplatcdn.qq.com/social_hall/js/jquery.js", "http://qqgameplatcdn.qq.com/social_hall/js/jquery.pm.js", "http://qqgameplatcdn.qq.com/social_hall/app_frame/qqgamelib.js"];
    }
    SPSdk.prototype.login = function (param, callback) {
        this.qgSdk.login(param, callback);
    };
    SPSdk.prototype.invite = function (param, callback) {
        this.qgSdk.invite(param, callback);
    };
    SPSdk.prototype.openVIPService = function (param, callback) {
        this.qgSdk.openVIPService(param, callback);
    };
    SPSdk.prototype.openVipAction = function (param, callback) {
        this.qgSdk.openVipAction(param, callback);
    };
    SPSdk.prototype.pay = function (param, callback) {
        this.qgSdk.pay(param, callback);
    };
    SPSdk.prototype.init = function (param, callback) {
        var _t = this;
        this.loadScrpits(this.urls, function () {
            _t.qgSdk = new QGSDK(param);
            if (!window["GameAPI"]) {
                window["GameAPI"] = new QQGameAPI();
                window["GameAPI"].init();
            }
            _t.qgSdk.gApi = window["GameAPI"];
            if (callback) {
                callback({ "result": 0, "desc": "OK", "sp_data": null });
            }
        });
    };
    SPSdk.prototype.loadScrpits = function (urls, callback) {
        var LayaCommon = window["LayaCommon"];
        var loaded = 0;
        function next() {
            LayaCommon.loadScript(urls[loaded], function () {
                loaded++;
                if (loaded >= urls.length) {
                    callback();
                }
                else {
                    next();
                }
            });
        }
        ;
        next();
    };
    return SPSdk;
}());
